package com.ms.game.sendgift.db;

import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.password.db.PasswordDisableTime;
import com.ms.game.password.db.PasswordErrorWay;
import com.ms.game.password.db.PasswordLogModel;
import com.ms.game.sendgift.GiftPlayerData;
import com.ms.game.sendgift.SendGiftStage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.Account;
import com.ms.service.module.account.AccountModule;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.db.model.PlayerModel;
import com.xpec.c4.game.time.Stamper;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;

public class checkPasswordForGiftDCLAsync extends CentralAsyncCommand {

	private static final long serialVersionUID = 392629039826483116L;
	/**
	* 20161003 Sean 
	* 為了網頁虛寶交易增加，讓Web摳的時候可以寫log
	*/
	public AccountModule accountModule;
	
	public GUID accountID;
	
	public GUID playerID;
	
	public String playName;
	
	public int stage;
	
	public String password;
	
	public PasswordErrorWay errorWay;
	
	public long orderID;
	
	private String bankPassword;
	
	/**
	 * 錯誤超過後等待時間
	 */
	private long disableTimes;
	
	/**
	 * 錯誤次數
	 */
	private int errorTimes;
	
	/**
	 * 解密後的密碼
	 */
	private String reductionPassword;
	
	@Override
	public void asyncExecute() throws Exception {
		
		Account accountData = AccountDAO.loadAccountByAccountId(accountID);

		String password = accountData.getBankPassword();
		if(password == null || password == ""){
			return;
		}

		if (stage == SendGiftStage.FIRST_CREATE) {
			bankPassword = password;
		} else if (stage != SendGiftStage.FIRST_CREATE) {

			reductionPassword = Stamper.getInstance().Reduction(password);

			if (reductionPassword == null) {
				return;
			}

			bankPassword = reductionPassword;
			PlayerModel model = PlayerDAO.getErrorTimesAndDisableTimeByGuid(playerID);
			disableTimes = model.getDisableTimeGift();
			errorTimes = model.getErrorTimesGift();
			
			if(disableTimes > TimeSys.currentTimeMills()){
				return;
			}
			
			if(!bankPassword.equals(this.password)){
				//輸入錯誤紀錄
				model.setErrorTimesGift(errorTimes + 1);
				errorTimes = model.getErrorTimesGift();
				if(model.getErrorTimesGift() == 5){
					model.setDisableTimeGift(TimeSys.currentTimeMills() + PasswordDisableTime.SENDGIFT_FIVE_TIMES_DISABLE_TIME);
					disableTimes = model.getDisableTimeGift();
				}else if(model.getErrorTimesGift() >= 10 && (model.getErrorTimesGift()%5)==0){
					model.setDisableTimeGift(TimeSys.currentTimeMills() + PasswordDisableTime.SENDGIFT_TEN_TIMES_DISABLE_TIME);
					disableTimes = model.getDisableTimeGift();
				}
				
				SendGiftNotifyModel notify = SendGiftDAO.loadSendGiftNotifyModelByOrderID(orderID);
				
				PasswordLogModel log = new PasswordLogModel();
				log.setOrderId(notify.getOrderID());
				log.setAccountId(accountID.toString());
				log.setPlayerId(playerID.toString());
				log.setPlayerName(playName);
				log.setPasswordEnter(password);
				log.setErrorTimes(model.getErrorTimes());
				log.setChangeWay(errorWay.getId());
				log.setChangeWayDesc(errorWay.getDesc());
				
				if(accountModule == null){
					//一般交易
					getCurrentModuleServer().saveDbLog(log);
				}else{
					//幫Web交易增加的
					accountModule.saveDbLog(log);
				}

				PlayerDAO.updatePlayerModelPasswordError(model);
			}else{
				//輸入正確歸0
				model.setErrorTimes(0);
				model.setDisableTime(0);
				model.setErrorTimesGift(0);
				model.setDisableTimeGift(0);
				PlayerDAO.updatePlayerModelPasswordError(model);
			}
		}
		
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		
		GameLobbyModule module = (GameLobbyModule)userData[0];
		SendGiftResponse response= (SendGiftResponse)userData[1];
		CentralAsyncCommand loadData = (CentralAsyncCommand)userData[2];
		int messageType = (int)userData[3];
		GUID sessionID = (GUID)userData[4];
		GiftPlayerData playerData = (GiftPlayerData)userData[5];

		//密碼判斷
		if(bankPassword == null || bankPassword == ""){
			//尚未建立密碼
			response.errorCode = ErrorCode.PASSWORD_CREATE_NOT_YET;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(stage == SendGiftStage.FINIAL_SEND){
			
			//解密是否有成功
			if(reductionPassword == null){
				response.errorCode = ErrorCode.DECRYPTION_FAILED;
				MessageAction.sendMessage(sessionID, messageType, response);
				return;
			}
			
			if(!bankPassword.equals(password)){
				Player playerSave= module.getZone().getPlayer(playerID);
				//如果在線上
				if(playerSave != null){
					//寫回zone player
					playerSave.setErrorTimesGift(errorTimes);
					playerSave.setDisableTimeGift(disableTimes);
				}
				
				if(disableTimes > TimeSys.currentTimeMills()){
					//記送密碼錯誤
					response.errorCode = ErrorCode.PASSWORD_ERROR_DISABLE_TIME;
					response.disableTime = disableTimes;
					MessageAction.sendMessage(sessionID, messageType, response);
					return;
				}
				
				//密碼錯誤
				response.errorCode = ErrorCode.PASSWORD_ERROR;
				response.errorTimes = errorTimes + 1;
				MessageAction.sendMessage(sessionID, messageType, response);
				return;
			}
			module.addCentralAsyncCommand(loadData,playerData);
		}
	}
	
	public String getBankPassword() {
		return bankPassword;
	}

	public long getDisableTimes() {
		return disableTimes;
	}

	public String getReductionPassword() {
		return reductionPassword;
	}

	public int getErrorTimes() {
		return errorTimes;
	}
}
